package com.wngbms.aop.utils;

import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

public class TargetFilterDemo {

    public static void main(String[] args) throws ClassNotFoundException {
        String className = "com.wngbms.aop.staticproxy.EchoService";
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        Class<?> targetClass = contextClassLoader.loadClass(className);

        Method echo = ReflectionUtils.findMethod(targetClass, "echo", String.class);
        System.out.println(echo);

        ReflectionUtils.doWithMethods(targetClass, new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                System.out.println(method);
            }
        }, new ReflectionUtils.MethodFilter() {
            @Override
            public boolean matches(Method method) {
                Class<?>[] exceptionTypes = method.getExceptionTypes();
                return exceptionTypes.length == 1 && exceptionTypes[0].equals(NullPointerException.class);
            }
        });

    }
}
